﻿<?php
class conndb {
        private $conn;
        private $result;
        private static $inst;

                public static function getInst() {
                        if(!isset(self::$inst))
                                echo "Error 03: Neexistuje spojenie s DB";
                        else
                                return self::$inst;
                }

        function __construct()  {
                if(!isset(self::$inst))
                        self::$inst = $this;
                else {
                        echo "Error 01: Databaza uz pripojena?";
                        exit;
                }

                @$this->conn = mysql_connect(DBSERVER,DBUSER,DBPASS);
                if($this->conn == false){
                        echo "Error 02: Pripojenie nebolo nadviazane!";
                        exit;
                }
                else {
                    $wow = mysql_select_db(DATABASE, $this->conn);
                    mysql_set_charset("utf8");
                        if($wow == false){
                            echo "Error 04: Zla databaza!";
                            exit;
                        }
                }
                }

        public function query($q){
            $this->result = mysql_query($q, $this->conn);
                }
        
        public function qresul(){
            return $this->result;
                }

        public function getNumResults() {
            return mysql_num_rows($this->result);
        }

        public function getSingleThing() {
            return mysql_result($this->result,0);
                }

        public function getRowAsObject() {
            return mysql_fetch_object($this->result);
                }

        public function getRowsAsArrayOfObjects(){
            $ar = array();
            while ($item = $this->getRowAsObject())
                array_push($ar, $item);                         
            return $ar;
                }
        
}

$conndb = New conndb(); // vytvori triedu na DB

class maincontroller {
        var $name = 'maincontroller';
        var $action = 'index';
                
        function heslo_check($log) {
            return preg_match("/^[a-zA-Z0-9]{5,20}$/", $log) > 0;
        }
        
        function meno_check($log) {
            $foo = iconv('utf-8', 'ASCII//IGNORE', $log);
            return preg_match("/^[[:alpha:]]{2,20}$/", $foo) > 0;
        }
       function nazov_check($log) {
            $foo = iconv('utf-8', 'ASCII//IGNORE', $log);
            return preg_match("/^[a-zA-Z0-9\s]{2,20}$/", $foo) > 0;
        }

        function long_text_check($txt) {
            $foo = iconv('utf-8', 'ASCII//IGNORE', $txt);
            return preg_match("/^[a-zA-Z0-9\s]{0,50}$/", $foo) > 0;
        }

        function number_check($numb) {
            return preg_match("/^[0-9]{1}$/", $numb) > 0;
        }

        function number_check2($numb) {
            return preg_match("/^[0-9]{1,}$/", $numb) > 0;
        }
                

        function mail_check($mail) {
            return preg_match("/^[a-zA-Z0-9\._-]{2,}@[a-zA-Z0-9\._-]{2,}\.[a-zA-Z]{2,4}$/", $mail) > 0;
        }
        public function redirect($path) {
            return view::load('redirect', array('path' => $path));
        }
        
        protected function copy_post_data($premenne, $data = array()) {
            foreach ($premenne as $prem) {
                if (isset($_POST[$prem])) {
                    $data[$prem] = $_POST[$prem];
                } else {
                    $data[$prem] = '';
                }
            }
            return $data;
        }
        
            function handle($action) {
            
            $this->action = $action;
            return false;
           
                 
                }
}
?>
